// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CMIRequest.java

package com.iphaeton.lms.course.scorm12.datamodel.cmi;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.StringTokenizer;

public class CMIRequest
    implements Serializable
{

    static void incrementPatternCount()
    {
        pattern_counter++;
    }

    static void resetPatternCounter()
    {
        pattern_counter = 2;
    }

    public CMIRequest(String theRequest, boolean getRequestFlag)
    {
        numSub = 10;
        numOfSubCategories = 0;
        numOfSubCatReturned = 0;
        numInd = 10;
        numOfInd = 0;
        numOfIndReturned = 0;
        totalNumberOfTokens = 0;
        tokensRequested = 1;
        tokensProcessed = 0;
        numPat = 10;
        if(_Debug)
            if(getRequestFlag)
                System.out.println("Building CMIRequest for a LMSGetValue(" + theRequest + ") ");
            else
                System.out.println("Building CMIRequest for a LMSSetValue(" + theRequest + ") ");
        request = theRequest;
        model = new String("");
        baseCategory = new String("");
        element = new String("");
        setValue = new String("");
        getRequest = getRequestFlag;
        index = new Integer[numInd];
        for(int i = 0; i < index.length; i++)
            index[i] = new Integer(-1);

        pattern = new Integer[numPat];
        for(int j = 0; j < pattern.length; j++)
            pattern[j] = new Integer(-1);

        subcategory = new String[numSub];
        for(int z = 0; z < subcategory.length; z++)
            subcategory[z] = new String("-1");

        String theSetValue = new String("");
        String theNewRequest = new String("");
        if(getRequest)
        {
            setRequest = false;
            parseGetRequest(theRequest);
        } else
        {
            setRequest = true;
            StringTokenizer setTok = new StringTokenizer(theRequest, ",", false);
            int numTokensForSet = setTok.countTokens();
            int numTokensProcessed = 0;
            theNewRequest = setTok.nextToken();
            numTokensProcessed++;
            if(_Debug)
                System.out.println("Request: [" + theNewRequest + "]");
            if(numTokensForSet == 1)
            {
                theSetValue = new String("");
            } else
            {
                while(numTokensProcessed < numTokensForSet) 
                {
                    theSetValue = theSetValue + setTok.nextToken();
                    if(++numTokensProcessed != numTokensForSet)
                        theSetValue = theSetValue + ",";
                }
                if(_Debug)
                    System.out.println("Set Value: [" + theSetValue + "]");
            }
            totalNumberOfTokens++;
            parseSetRequest(theNewRequest, theSetValue);
        }
        if(_Debug)
            showRequest();
    }

    public String getModel()
    {
        return model;
    }

    public boolean isForASetRequest()
    {
        return setRequest;
    }

    public boolean isAKeywordRequest()
    {
        boolean result = false;
        if(isAChildrenRequest() || isACountRequest() || isAVersionRequest())
            result = true;
        return result;
    }

    public boolean isAChildrenRequest()
    {
        boolean result = false;
        if(element.equals("_children"))
            result = true;
        return result;
    }

    public boolean isACountRequest()
    {
        boolean result = false;
        if(element.equals("_count"))
            result = true;
        return result;
    }

    public boolean isAVersionRequest()
    {
        boolean result = false;
        if(element.equals("_version"))
            result = true;
        return result;
    }

    public String getRequest()
    {
        return request;
    }

    public String getBaseCategory()
    {
        tokensRequested++;
        return baseCategory;
    }

    public String getValue()
    {
        return setValue;
    }

    public String getElement()
    {
        return element;
    }

    public int getTotalNumTokens()
    {
        return totalNumberOfTokens;
    }

    public int getNumSubCat()
    {
        return numOfSubCategories;
    }

    public int getNumIndices()
    {
        return numOfInd;
    }

    public Integer getIndex(int position)
    {
        return index[position];
    }

    public String getSubCategory(int position)
    {
        String subCat = new String("-1");
        if(position >= 0 && position < 10)
            subCat = subcategory[position];
        return subCat;
    }

    private void parseGetRequest(String theRequest)
    {
        StringTokenizer stk = new StringTokenizer(theRequest, ".", false);
        totalNumberOfTokens += stk.countTokens();
        model = stk.nextToken();
        tokensProcessed++;
        pattern[0] = new Integer(MODEL);
        baseCategory = stk.nextToken();
        tokensProcessed++;
        pattern[1] = new Integer(BASE_CATEGORY);
        if(stk.hasMoreTokens())
        {
            for(boolean done = false; !done;)
                if(onLastToken())
                {
                    String temp = stk.nextToken();
                    try
                    {
                        Integer tmpInt = new Integer(temp);
                        int tmpIndex = findNextIndexLoc();
                        index[tmpIndex] = tmpInt;
                        numOfInd++;
                        int tmpIndex1 = findNextPatternLoc();
                        pattern[tmpIndex1] = new Integer(ARRAY_INDEX);
                        element = subcategory[numOfSubCategories - 1];
                        tmpIndex1 = findNextPatternLoc();
                        pattern[tmpIndex1] = new Integer(ELEMENT);
                    }
                    catch(NumberFormatException nfe)
                    {
                        element = temp;
                        int tmpIndex = findNextPatternLoc();
                        pattern[tmpIndex] = new Integer(ELEMENT);
                    }
                    tokensProcessed++;
                    done = true;
                } else
                {
                    determineNextToken(stk);
                }

        } else
        {
            element = baseCategory;
        }
    }

    private void parseSetRequest(String theRequest, String theSetValue)
    {
        if(_Debug)
            System.out.println("In CMIRequest::parseSetRequest");
        StringTokenizer stk = new StringTokenizer(theRequest, ".", false);
        totalNumberOfTokens += stk.countTokens();
        model = stk.nextToken();
        tokensProcessed++;
        pattern[0] = new Integer(MODEL);
        baseCategory = stk.nextToken();
        tokensProcessed++;
        pattern[1] = new Integer(BASE_CATEGORY);
        if(stk.hasMoreTokens())
        {
            for(boolean done = false; !done;)
                if(onLastToken())
                {
                    setValue = theSetValue;
                    tokensProcessed++;
                    int tmpIndex = findNextPatternLoc();
                    pattern[tmpIndex] = new Integer(VALUE);
                    done = true;
                } else
                if(totalNumberOfTokens - tokensProcessed == 2)
                {
                    element = stk.nextToken();
                    tokensProcessed++;
                    int tmpIndex = findNextPatternLoc();
                    pattern[tmpIndex] = new Integer(ELEMENT);
                } else
                {
                    determineNextToken(stk);
                }

        } else
        {
            setValue = theSetValue;
            tokensProcessed++;
            int tmpIndex = findNextPatternLoc();
            pattern[tmpIndex] = new Integer(VALUE);
        }
    }

    private void determineNextToken(StringTokenizer stk)
    {
        String temp = stk.nextToken();
        tokensProcessed++;
        try
        {
            Integer tmpInt = new Integer(temp);
            int tmpIndex = findNextIndexLoc();
            index[tmpIndex] = tmpInt;
            numOfInd++;
            int tmpIndex1 = findNextPatternLoc();
            pattern[tmpIndex1] = new Integer(ARRAY_INDEX);
        }
        catch(NumberFormatException nfe)
        {
            int tmpIndex = findNextSubLoc();
            subcategory[tmpIndex] = temp;
            numOfSubCategories++;
            int tmpIndex1 = findNextPatternLoc();
            pattern[tmpIndex1] = new Integer(SUB_CATEGORY);
        }
    }

    private boolean onLastToken()
    {
        boolean rtrnFlag = false;
        if(totalNumberOfTokens - tokensProcessed == 1)
            rtrnFlag = true;
        return rtrnFlag;
    }

    public String getNextToken()
    {
        String rtrnValue = new String("");
        try
        {
            Integer tmpPat = pattern[pattern_counter];
            int tmpPattern = tmpPat.intValue();
            if(tmpPattern == SUB_CATEGORY)
                try
                {
                    rtrnValue = subcategory[numOfSubCatReturned];
                    numOfSubCatReturned++;
                }
                catch(IndexOutOfBoundsException ioobe)
                {
                    System.out.println(ioobe);
                }
            else
            if(tmpPattern == ARRAY_INDEX)
                try
                {
                    Integer tmpIndex = index[numOfIndReturned];
                    numOfIndReturned++;
                    rtrnValue = tmpIndex.toString();
                }
                catch(IndexOutOfBoundsException ioobe)
                {
                    System.out.println(ioobe);
                }
            else
            if(tmpPattern == ELEMENT)
            {
                rtrnValue = element;
                resetPatternCounter();
            } else
            if(tmpPattern == VALUE)
                rtrnValue = setValue;
            if(tmpPattern != ELEMENT)
                incrementPatternCount();
            tokensRequested++;
        }
        catch(IndexOutOfBoundsException ioobe)
        {
            System.out.println(ioobe);
        }
        return rtrnValue;
    }

    public boolean hasMoreTokensToProcess()
    {
        boolean rtrnFlag = true;
        if(setRequest)
        {
            if(totalNumberOfTokens - 1 - tokensRequested == 0)
                rtrnFlag = false;
        } else
        if(totalNumberOfTokens - tokensRequested == 0)
            rtrnFlag = false;
        return rtrnFlag;
    }

    public void done()
    {
        resetPatternCounter();
        tokensRequested = 1;
        numOfSubCatReturned = 0;
        numOfIndReturned = 0;
    }

    private int findNextPatternLoc()
    {
        int result = -1;
        int value = -1;
        for(int i = 0; i <= numPat; i++)
        {
            if(value != pattern[i].intValue())
                continue;
            result = i;
            break;
        }

        return result;
    }

    private int findNextIndexLoc()
    {
        int result = -1;
        int value = -1;
        for(int i = 0; i <= numInd; i++)
        {
            if(value != index[i].intValue())
                continue;
            result = i;
            break;
        }

        return result;
    }

    private int findNextSubLoc()
    {
        int result = -1;
        String tmpString = new String("-1");
        for(int i = 0; i <= numSub; i++)
        {
            if(!subcategory[i].equalsIgnoreCase(tmpString))
                continue;
            result = i;
            break;
        }

        return result;
    }

    public void showRequest()
    {
        if(_Debug)
        {
            System.out.println("Base Category: " + baseCategory);
            System.out.println("Element: " + element);
            System.out.println("Model: " + model);
            System.out.println("Number of Indices: " + numOfInd);
            System.out.println("Number of SubCategories: " + numOfSubCategories);
            for(int i = 0; i < numOfSubCategories; i++)
                System.out.println("Subcategory[" + i + "]: " + subcategory[i]);

            for(int j = 0; j < numOfInd; j++)
                System.out.println("Index[" + j + "]: " + index[j]);

            for(int x = 0; x < numPat; x++)
                if(pattern[x].intValue() > -1)
                    System.out.println("Pattern[" + x + "]: " + pattern[x]);

            if(isForASetRequest())
                System.out.println("Set Value: " + setValue);
        }
    }

    private static final long serialVersionUID = 0xc1f860f0cf9ddcfbL;
    private static boolean _Debug;
    private String request;
    private String model;
    private String baseCategory;
    private int numSub;
    private String subcategory[];
    private int numOfSubCategories;
    private int numOfSubCatReturned;
    private int numInd;
    private Integer index[];
    private int numOfInd;
    private int numOfIndReturned;
    private String element;
    private String setValue;
    private int totalNumberOfTokens;
    private int tokensRequested;
    private int tokensProcessed;
    private boolean getRequest;
    private boolean setRequest;
    private int numPat;
    private Integer pattern[];
    private static int MODEL = 1;
    private static int BASE_CATEGORY = 2;
    private static int SUB_CATEGORY = 3;
    private static int ARRAY_INDEX = 4;
    private static int ELEMENT = 5;
    private static int VALUE = 6;
    static int pattern_counter = 2;

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
